clear all

	

**************************************************************************
****************** FOR ALL IPV STATEMENTS
**************************************************************************

use "$data_folder/survey_data/Mujeres_1Ronda_RDD_HPI_checked_completed.dta", clear

sort caseid starttime
br ipv_v1_1 ipv_v1_1_c ipv_v1_1_cc*
sort ipv_v1_1 ipv_v1_1_c ipv_v1_1_cc*

/*
. label list ipv_old_1
ipv_v1_1:
          1 It didn't happen in 2019
           2 One time
           3 Sometimes
           4 Many times
         888 Refused to answer
         999 Do not know


. label list ipv_old_1_c
ipv_v1_1_c:
          1 It didn't happen in 2019
           2 At least once a month (12+ times)
           3 At least once every two months (6-12 times)
           4 At least once every 6 months
           5 Only one time
         888 Refused to answer
         999 Do not know


. label list ipv_old_1
ipv_v1_1_cc:
          1 It didn't happen in 2019
           2 One time
           3 Sometimes
           4 Many times
         888 Refused to answer
         999 Do not know
*/

//need to add the zero cases in 2019 here 


forvalues i=1/8 {

		recode ipv_v`i'_1 (1 = 0 "Nunca") (2 = 1 "1 vez") (3 4 = 2 "2 veces"), gen (x`i')
		drop ipv_v`i'_1
		rename x`i' ipv_v`i'_1
		recode ipv_v`i'_1_c (1 = 0 "Nunca") (5 = 1 "1 vez") (4 = 2 "2 veces") (3 = 6 "6 veces") (2 = 12 "12 veces"), gen (x`i'_c)
		drop ipv_v`i'_1_c
		rename x`i'_c ipv_v`i'_1_c
		recode ipv_v`i'_1_cc (1 = 0 "Nunca") (2 = 1 "1 vez") (3 4 = 2 "2 veces"), gen (x`i'_cc)
		drop ipv_v`i'_1_cc
		rename x`i'_cc ipv_v`i'_1_cc
		order ipv_v`i'_1_c ipv_v`i'_1_cc, after(ipv_v`i'_1)
		egen ipv`i'_reg2019 = rowtotal(ipv_v`i'_1 ipv_v`i'_1_c ipv_v`i'_1_cc),  m
		tab ipv`i'_reg2019

		*1.2 Violence during april_may 2020
		// ipv_v`i'_2 refers to if there is any IPV in April 2020
		// We first code this as 1 0
		gen ipv`i'_reg2020=1 if ipv_v`i'_2==1
		replace ipv`i'_reg2020=0 if ipv_v`i'_2==0
		
		
		// ipv_v`i'_3 refers to if there was 1  or more cases of IPV during april 2020. 
		// We now code  ipv`i'_reg2020 to include 2 and 1. 
		replace ipv`i'_reg2020=1 if ipv_v`i'_3==1
		replace ipv`i'_reg2020=2 if ipv_v`i'_3==2 | ipv_v`i'_3==3
	
	

		
		
		// the next blocks of code rely on that  ipv_v`i'_4 is not missing only for people with  ipv_v`i'_2 != 0 
		// and that if you reported zero IPV in 2019 then ipv_v`i'_4 is missing 
		// ANd also  ipv_v1_4 was only asked for a few participants before relying on the last two question s
		// ipv_v`i'_3 and ipv_v`i'_2
		// so we only need to replace the 2020 value for the people who have a non missing ipv_v`i'_4
		
		*Menos than 2019
		
		replace ipv`i'_reg2020=0 if ipv_v`i'_4==1 & ipv`i'_reg2019==1 
		replace ipv`i'_reg2020=1 if ipv_v`i'_4==1 & ipv`i'_reg2019==2
		replace ipv`i'_reg2020=1 if ipv_v`i'_4==1 & ipv`i'_reg2019==6
		replace ipv`i'_reg2020=1 if ipv_v`i'_4==1 & ipv`i'_reg2019==12

		*replace ipv1_reg2020=0.5 if ipv_v1_4_c==1 & ipv1_reg2019==1
		replace ipv`i'_reg2020= 0 if ipv_v`i'_4_c==1 & ipv`i'_reg2019==1
		replace ipv`i'_reg2020= 1 if ipv_v`i'_4_c==1 & ipv`i'_reg2019==2
		replace ipv`i'_reg2020= 1 if ipv_v`i'_4_c==1 & ipv`i'_reg2019==6
		replace ipv`i'_reg2020= 1 if ipv_v`i'_4_c==1 & ipv`i'_reg2019==12


		
		*Equal than 2019
		replace ipv`i'_reg2020 = 1 if  ipv_v`i'_4==2 & ipv`i'_reg2019==1
		replace ipv`i'_reg2020 = 1 if  ipv_v`i'_4==2 & ipv`i'_reg2019==2
		replace ipv`i'_reg2020 = 1 if  ipv_v`i'_4==2 & ipv`i'_reg2019==6
		replace ipv`i'_reg2020 = 1 if ipv_v`i'_4==2 & ipv`i'_reg2019==12

		replace ipv`i'_reg2020 = 1 if  ipv_v`i'_4_c==2 & ipv`i'_reg2019==1
		replace ipv`i'_reg2020 = 1 if  ipv_v`i'_4_c==2 & ipv`i'_reg2019==2
		replace ipv`i'_reg2020 = 1 if  ipv_v`i'_4_c==2 & ipv`i'_reg2019==6
		replace ipv`i'_reg2020 = 1 if ipv_v`i'_4_c==2 & ipv`i'_reg2019==12


		
		*More than 2019
		replace ipv`i'_reg2020= 1  if ipv_v`i'_4==3 & ipv`i'_reg2019==1
		replace ipv`i'_reg2020= 1  if ipv_v`i'_4==3 & ipv`i'_reg2019==2
		replace ipv`i'_reg2020= 1  if ipv_v`i'_4==3 & ipv`i'_reg2019==6
		replace ipv`i'_reg2020= 2   if ipv_v`i'_4==3 & ipv`i'_reg2019==12
		
		replace ipv`i'_reg2020= 1 if  ipv_v`i'_4_c==3 & ipv`i'_reg2019==1
		replace ipv`i'_reg2020= 1 if  ipv_v`i'_4_c==3 & ipv`i'_reg2019==2
		replace ipv`i'_reg2020= 1 if  ipv_v`i'_4_c==3 & ipv`i'_reg2019==6
		replace ipv`i'_reg2020= 2 if  ipv_v`i'_4_c==3 & ipv`i'_reg2019==12

		 

		*1.3 Violence during july-august 2020
		
		

		//this should be true
		assert ipv_v`i'_4_c == . | ipv_v`i'_4 == .  if ipv_v`i'_2 == 0


		//in previous cases do not need to worry about ipv`i'_reg2019==0 because ipv_v`i'_4_c, ipv_v`i'_4 are missing if ipv`i'_reg2019==0
		
		assert ipv_v`i'_4_c == . |  ipv_v`i'_4 == . if ipv`i'_reg2019==0 		
		
		
		
		
		
		// ipv_v`i'_5 refers to if there is any IPV in July 2020
		//We first code this as 1 0		
		gen ipv`i'_reg2021=1 if ipv_v`i'_5==1
		replace ipv`i'_reg2021=0 if ipv_v`i'_5==0

		// ipv_v`i'_6 refers to if there was 1  or more cases of IPV during July 2020. 
		//We now code  ipv`i'_reg2020 to include 2 and 1. 
				replace ipv`i'_reg2021=1 if ipv_v`i'_6==1
				replace ipv`i'_reg2021=2 if ipv_v`i'_6==2 | ipv_v`i'_6==3

				
				
				//the next blocks of code rely on that  ipv_v`i'_7 is not missing only for people with  ipv_v`i'_5 != 0 				
				*Menos than 2019
				
				*replace ipv1_reg2020=0.5 if ipv_v1_6==1 & ipv1_reg2019==1
				replace ipv`i'_reg2021=0 if ipv_v`i'_7==1 & ipv`i'_reg2019==1
				replace ipv`i'_reg2021=1 if ipv_v`i'_7==1 & ipv`i'_reg2019==2
				replace ipv`i'_reg2021=1 if ipv_v`i'_7==1 & ipv`i'_reg2019==6
				replace ipv`i'_reg2021=1 if ipv_v`i'_7==1 & ipv`i'_reg2019==12

				*replace ipv1_reg2020=0.5 if ipv_v1_6_c==1 & ipv1_reg2019==1
				replace ipv`i'_reg2021=0 if ipv_v`i'_7_c==1 & ipv`i'_reg2019==1
				replace ipv`i'_reg2021=1 if ipv_v`i'_7_c==1 & ipv`i'_reg2019==2
				replace ipv`i'_reg2021=1 if ipv_v`i'_7_c==1 & ipv`i'_reg2019==6
				replace ipv`i'_reg2021=1 if ipv_v`i'_7_c==1 & ipv`i'_reg2019==12
				

				*Equal than 2019
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7==2 & ipv`i'_reg2019==1
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7==2 & ipv`i'_reg2019==2
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7==2 & ipv`i'_reg2019==6
				replace ipv`i'_reg2021=1 if ipv_v`i'_7==2 & ipv`i'_reg2019==12

				replace ipv`i'_reg2021=1 if  ipv_v`i'_7_c==2 & ipv`i'_reg2019==1
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7_c==2 & ipv`i'_reg2019==2
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7_c==2 & ipv`i'_reg2019==6
				replace ipv`i'_reg2021=1 if ipv_v`i'_7_c==2 & ipv`i'_reg2019==12


				*More than 2019
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7==3 & ipv`i'_reg2019==1
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7==3 & ipv`i'_reg2019==2
				replace ipv`i'_reg2021=1 if ipv_v`i'_7==3 & ipv`i'_reg2019==6
				replace ipv`i'_reg2021=2 if ipv_v`i'_7==3 & ipv`i'_reg2019==12

				replace ipv`i'_reg2021=1 if  ipv_v`i'_7_c==3 & ipv`i'_reg2019==1
				replace ipv`i'_reg2021=1 if  ipv_v`i'_7_c==3 & ipv`i'_reg2019==2
				replace ipv`i'_reg2021=1 if ipv_v`i'_7_c==3 & ipv`i'_reg2019==6
				replace ipv`i'_reg2021=2 if ipv_v`i'_7_c==3 & ipv`i'_reg2019==12
				
		
		//this should be true
		assert ipv_v`i'_7_c == . | ipv_v`i'_7 == .  if ipv_v`i'_5 == 0
		
		//in previous cases do not need to worry about ipv`i'_reg2019==0 because ipv_v`i'_7_c, ipv_v`i'_7 are missing if ipv`i'_reg2019==0
		
		assert ipv_v`i'_7_c == . |  ipv_v`i'_7 == . if ipv`i'_reg2019==0 
		
		//añadir una varibale que nos diga que tipo de pregunta hicimos 
		

		*1.4 Reshape. We create dependent variable for each period (2019, AM2020,JA2020)
		br caseid ipv`i'_reg*
		sort caseid

			preserve
				gen id_cases=caseid
				destring id_cases, replace
				reshape long ipv`i'_reg, i(id_cases) j(year)
				tempfile a`i'
				save `a`i''
			restore
}


gen id_cases=caseid
destring id_cases, replace

use `a1', clear
merge 1:1 caseid year using `a2', nogen
merge 1:1 caseid year using `a3', nogen
merge 1:1 caseid year using `a4', nogen
merge 1:1 caseid year using `a5', nogen
merge 1:1 caseid year using `a6', nogen 
merge 1:1 caseid year using `a7', nogen
merge 1:1 caseid year using `a8', nogen


br ipv*_reg

*1,5 Dummy for period
sort id_cases year
gen dummy_time=0 if year==2019
replace dummy_time=1 if year==2020 | year==2021

*1.6 Generate two samples
gen sample_reg1=1 if year==2019 | year==2020
gen sample_reg2=1 if year==2019 | year==2021 


*1.6 Weighting variable
gen month=6 if year==2019
replace month=1 if year==2020 | year==2021

sort id_cases sample_reg1



*****************************************************************************
**********COVARIATES
*****************************************************************************

**Income 2019 and 2020
*Middle bound woman
gen income_woman_mid=income_2019_woman if year==2019
replace income_woman_mid=income_2020am_woman if year==2020
replace income_woman_mid=income_2020ja_woman if year==2021

	replace income_woman_mid=0    if (income_2019_woman_r==1) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=250  if (income_2019_woman_r==2) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=750  if (income_2019_woman_r==3) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=1250 if (income_2019_woman_r==4) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=1750 if (income_2019_woman_r==5) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=2250 if (income_2019_woman_r==6) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=2750 if (income_2019_woman_r==7) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=3250 if (income_2019_woman_r==8) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=3750 if (income_2019_woman_r==9) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=4250 if (income_2019_woman_r==10) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=4750 if (income_2019_woman_r==11) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=5000 if (income_2019_woman_r==12) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=. 	  if (income_2019_woman_r==999) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019
	replace income_woman_mid=.    if (income_2019_woman_r==888) & (income_2019_woman==777 |  income_2019_woman==888 | income_2019_woman==999) & year==2019

	replace income_woman_mid=0    if (income_2020am_woman_r==1) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=250  if (income_2020am_woman_r==2) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=750  if (income_2020am_woman_r==3) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=1250 if (income_2020am_woman_r==4) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=1750 if (income_2020am_woman_r==5) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=2250 if (income_2020am_woman_r==6) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=2750 if (income_2020am_woman_r==7) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=3250 if (income_2020am_woman_r==8) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=3750 if (income_2020am_woman_r==9) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=4250 if (income_2020am_woman_r==10) & (income_2020am_woman==777 | income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=4750 if (income_2020am_woman_r==11) & (income_2020am_woman==777 | income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=5000 if (income_2020am_woman_r==12) & (income_2020am_woman==777 | income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=. 	  if (income_2020am_woman_r==999) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020
	replace income_woman_mid=.    if (income_2020am_woman_r==888) & (income_2020am_woman==777 |  income_2020am_woman==888 | income_2020am_woman==999) & year==2020

	replace income_woman_mid=0    if (income_2020ja_woman_r==1) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=250  if (income_2020ja_woman_r==2) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=750  if (income_2020ja_woman_r==3) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=1250 if (income_2020ja_woman_r==4) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=1750 if (income_2020ja_woman_r==5) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=2250 if (income_2020ja_woman_r==6) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=2750 if (income_2020ja_woman_r==7) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=3250 if (income_2020ja_woman_r==8) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=3750 if (income_2020ja_woman_r==9) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=4250 if (income_2020ja_woman_r==10) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=4750 if (income_2020ja_woman_r==11) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=5000 if (income_2020ja_woman_r==12) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=. 	  if (income_2020ja_woman_r==999) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021
	replace income_woman_mid=.    if (income_2020ja_woman_r==888) & (income_2020ja_woman==777 |  income_2020ja_woman==888 | income_2020ja_woman==999) & year==2021


**Middle bound man
gen income_man_mid=.

	replace income_man_mid=0    if (income_2019_partner_r==1) 	& year==2019
	replace income_man_mid=250  if (income_2019_partner_r==2) 	& year==2019
	replace income_man_mid=750  if (income_2019_partner_r==3) 	& year==2019
	replace income_man_mid=1250 if (income_2019_partner_r==4) 	& year==2019
	replace income_man_mid=1750 if (income_2019_partner_r==5) 	& year==2019
	replace income_man_mid=2250 if (income_2019_partner_r==6) 	& year==2019
	replace income_man_mid=2750 if (income_2019_partner_r==7) 	& year==2019
	replace income_man_mid=3250 if (income_2019_partner_r==8) 	& year==2019
	replace income_man_mid=3750 if (income_2019_partner_r==9) 	& year==2019
	replace income_man_mid=4250 if (income_2019_partner_r==10)	& year==2019
	replace income_man_mid=4750 if (income_2019_partner_r==11)	& year==2019
	replace income_man_mid=5000 if (income_2019_partner_r==12)	& year==2019
	replace income_man_mid=.    if (income_2019_partner_r==999)  & year==2019
	replace income_man_mid=.    if (income_2019_partner_r==888)  & year==2019

	replace income_man_mid=0    if (income_2020am_partner_r==1) 	& year==2020
	replace income_man_mid=250  if (income_2020am_partner_r==2) 	& year==2020
	replace income_man_mid=750  if (income_2020am_partner_r==3) 	& year==2020
	replace income_man_mid=1250 if (income_2020am_partner_r==4) 	& year==2020
	replace income_man_mid=1750 if (income_2020am_partner_r==5) 	& year==2020
	replace income_man_mid=2250 if (income_2020am_partner_r==6) 	& year==2020
	replace income_man_mid=2750 if (income_2020am_partner_r==7) 	& year==2020
	replace income_man_mid=3250 if (income_2020am_partner_r==8) 	& year==2020
	replace income_man_mid=3750 if (income_2020am_partner_r==9) 	& year==2020
	replace income_man_mid=4250 if (income_2020am_partner_r==10)	& year==2020
	replace income_man_mid=4750 if (income_2020am_partner_r==11)	& year==2020
	replace income_man_mid=5000 if (income_2020am_partner_r==12)	& year==2020
	replace income_man_mid=.    if (income_2020am_partner_r==999)  & year==2020
	replace income_man_mid=.    if (income_2020am_partner_r==888)  & year==2020

	replace income_man_mid=0    if (income_2020ja_partner_r==1) 	& year==2021
	replace income_man_mid=250  if (income_2020ja_partner_r==2) 	& year==2021
	replace income_man_mid=750  if (income_2020ja_partner_r==3) 	& year==2021
	replace income_man_mid=1250 if (income_2020ja_partner_r==4) 	& year==2021
	replace income_man_mid=1750 if (income_2020ja_partner_r==5) 	& year==2021
	replace income_man_mid=2250 if (income_2020ja_partner_r==6) 	& year==2021
	replace income_man_mid=2750 if (income_2020ja_partner_r==7) 	& year==2021
	replace income_man_mid=3250 if (income_2020ja_partner_r==8) 	& year==2021
	replace income_man_mid=3750 if (income_2020ja_partner_r==9) 	& year==2021
	replace income_man_mid=4250 if (income_2020ja_partner_r==10)	& year==2021
	replace income_man_mid=4750 if (income_2020ja_partner_r==11)	& year==2021
	replace income_man_mid=5000 if (income_2020ja_partner_r==12)	& year==2021
	replace income_man_mid=.    if (income_2020ja_partner_r==999)  & year==2021
	replace income_man_mid=.    if (income_2020ja_partner_r==888)  & year==2021



***************************************************************8
****** Intensity of lockdown
****************************************************************

***Generating self reported lockdown_activities
*i) Shopping
gen shopping=pre_lockdown_activities if year==2019
replace shopping=lockdown_activities if year==2020
replace shopping=post_lockdown_activities if year==2021

*ii) Socialize
gen socialize=pre_lockdown_socialize if year==2019
replace socialize=lockdown_socialize if year==2020
replace socialize=post_lockdown_socialize if year==2021



***************************************************************8
****** LABOR MEASURES
****************************************************************
*i) Woman 
sort caseid year
br caseid year dummy_time
*gen worked_woman=(work_periods_1==1) if year==2019
*replace worked_woman=(work_periods_2==1) if year==2020
*replace worked_woman=(work_periods_3==1) if year==2021
gen worked_woman=1 if income_woman_mid>0 & income_woman_mid!=. 
replace worked_woman=0 if income_woman_mid<=0 & income_woman_mid!=.

*ii)Men
*gen worked_man=(work_partner==1) if year==2020
*replace worked_man=. if (work_partner==.)  & year==2020
*replace worked_man=work_partner if (work_partner==.d)  & year==2020
*replace worked_man=work_partner if (work_partner==.r)  & year==2020
gen worked_man=1 if income_man_mid>0 & income_man_mid!=.
replace worked_man=0 if income_man_mid<=0 & income_man_mid!=.



*iii)Work from home 
replace home_office=. if (home_office==0 | home_office==1) & (year==2019 | year==2021) 
/*
*a) Woman
gen wfh_woman=1 if work_periods_2==1 & home_office==1 & year==2020
replace wfh_woman=0 if work_periods_2==1 & home_office==0 & year==2020
replace wfh_woman=0 if work_periods_2==0 & year==2020
**Assuming no one wfh during 2019
replace wfh_woman=0 if work_periods_1==1 & year==2019
replace wfh_woman=0 if work_periods_1==0 & year==2019
tab worked_woman  wfh_woman if year==2019 , m
tab worked_woman  wfh_woman if year==2020 , m //55missing
*/
gen wfh_woman=1 if worked_woman==1 & home_office==1 & year==2020
replace  wfh_woman=0 if worked_woman==1 & home_office==0 & year==2020
replace  wfh_woman=0 if worked_woman==0 & year==2020
**Assuming no one wfh during 2019
replace wfh_woman=0 if worked_woman==1 & year==2019
replace wfh_woman=0 if worked_woman==0 & year==2019

*b) man
/*
gen wfh_man=1 if work_partner==1 & home_office_partner==1 & year==2020
replace wfh_man=0 if work_partner==1 & home_office_partner==0 & year==2020
*/
**Correction
replace home_office_partner=. if worked_man==.

gen wfh_man=1 if worked_man==1 & home_office_partner==1 & year==2020
replace wfh_man=0 if worked_woman==0 & home_office_partner==0 & year==2020
**Assuming no one wfh during 2019
replace wfh_man=0 if worked_man==1 & year==2019
replace wfh_man=0 if worked_man==0 & year==2019


*a) Work outside home
/*
gen woh_woman=1 if work_periods_2==1 & home_office==0 & year==2020
replace woh_woman=0 if work_periods_2==1 & home_office==1 & year==2020
replace woh_woman=0 if work_periods_2==0 & year==2020
**Assuming everyone woh during 2019
replace woh_woman=1 if work_periods_1==1 & year==2019
replace woh_woman=0 if work_periods_1==0 & year==2019
*/
gen woh_woman=1 if worked_woman==1 & home_office==0 & year==2020
replace woh_woman=0 if worked_woman==1 & home_office==1 & year==2020
replace woh_woman=0 if worked_woman==0 & year==2020
**Assuming everyone woh during 2019
replace woh_woman=1 if worked_woman==1 & year==2019
replace woh_woman=0 if worked_woman==0 & year==2019

*man
gen woh_man=1 if worked_man==1 & home_office_partner==0 & year==2020
replace woh_man=0 if worked_man==1 & home_office_partner==1 & year==2020
replace woh_man=0 if worked_man==0 & year==2020
**Assuming everyone woh during 2019
replace woh_man=1 if worked_man==1 & year==2019
replace woh_man=0 if worked_man==0 & year==2019


/*
**55 Observations we dont know if they wfh during April/May
drop if wfh_woman==. & worked_woman==1 & year==2020
bysort caseid: keep if _N>2
** Observations we dont know if they wfh during April/May
drop if wfh_man==. & worked_man==1 & year==2020
bysort caseid: keep if _N>2
*/

gen top_earner2019=1 if (income_man_mid>income_woman_mid) & year==2019
replace top_earner2019=0 if (income_man_mid<=income_woman_mid) & year==2019
replace top_earner2019=. if (income_woman_mid==. | income_man_mid==.) & year==2019 
sort caseid top_earner2019
bysort caseid: replace top_earner2019=top_earner2019[_n-1] if missing(top_earner2019)
label define top_earner 1 "Man" 0 "Woman"
label values top_earner2019 top_earner 


gen top_earner2020=1 if (income_man_mid>income_woman_mid) & year==2020
replace top_earner2020=0 if (income_man_mid<=income_woman_mid) & year==2020
replace top_earner2020=. if (income_woman_mid==. | income_man_mid==.) & year==2020
sort caseid top_earner2020
bysort caseid: replace top_earner2020=top_earner2020[_n-1] if missing(top_earner2020)
label values top_earner2020 top_earner

**Top earner is no longer top earner
gen top_earner_2020_2019=1 if top_earner2019==0 & top_earner2020==0
replace top_earner_2020_2019=2 if top_earner2019==0 & top_earner2020==1
replace top_earner_2020_2019=3 if top_earner2019==1 & top_earner2020==1
replace top_earner_2020_2019=4 if top_earner2019==1 & top_earner2020==0

label define top_earner_2020_2019 1 "Woman-Woman" 2 "Woman-Man" 3 "Man-Man" 4 "Man-Woman"
label values top_earner_2020_2019 top_earner_2020_2019



***************************************************************8
****** HH
****************************************************************

*Alcohol consumption 
tab alcohol_partner_aprmay
tab alcohol_partner_2019
gen alcohol=1 if alcohol_partner_2019>=2 & alcohol_partner_2019<=6 & year==2019
replace alcohol=0 if alcohol_partner_2019==1 & year==2019
replace alcohol=1 if alcohol_partner_aprmay>=2 & alcohol_partner_aprmay<=6 & year==2020
replace alcohol=0 if alcohol_partner_aprmay==1 & year==2020

** Hacinamiento
// gen crowded=1 if (hh_members/hh_rooms_lockdown)>2.5
// replace crowded=0 if missing(crowded)
// replace crowded=. if (hh_members==.r | hh_rooms_lockdown==.r) 
// replace crowded=. if (hh_members==.d | hh_rooms_lockdown==.d) 


*Spanish mother tongue
gen  mother_tongue_reg=(mother_tongue==5)

*Children_N
*tab children_lockdown
*replace children_lockdown=0 if hh_members==hh_adults

*Benefits COVID
*tab benefits_april_may
*replace benefits_april_may=0 if year==2019


***Var labels
label var dummy_time "Dummy-Time"
label var income_woman_mid "Income(woman)"
label var income_man_mid "Income(man)"


//top code to two 
foreach var in any_violence phy_violence psy_violence cont_behavior arguments  {
	
	
}





save "$data_folder/NBER_HPI_PANEL.dta", replace

